package com.lenovo.testreport.user.service;

import com.lenovo.testreport.user.mapper.MessageCenterMapper;
import com.lenovo.testreport.user.model.MessageCenter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author CJF
 * @date 2018/10
 */
@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MessageCenterMapper mapper;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Override
    public List<MessageCenter> FindAllMessageRecord() {
        RedisSerializer redisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(redisSerializer);
        String key = "AllMessageRecord";
        List<MessageCenter> List = (List<MessageCenter>) redisTemplate.opsForValue().get(key);
        if (List == null) {
            List = mapper.selectAll();
            redisTemplate.opsForValue().set(key, List, 120, TimeUnit.SECONDS);
            //  System.out.println(key+" 缓存中没找到，访问数据库！");
        }
        //  else System.out.println(key+" 缓存中找到了，无需访问数据库！");
        return List;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int AddMessageRecord(MessageCenter msg) {
        String key = "AllMessageRecord";
        String type = msg.getType();
        if (type.contains("user")) {
            redisTemplate.delete("MessageRecord/user");
        }
        if (type.contains("admin")) {
            redisTemplate.delete("MessageRecord/admin");
        }
        if (type.contains("normal")) {
            redisTemplate.delete("MessageRecord/normal");
        }
        if (type.contains("jenkins")) {
            redisTemplate.delete("MessageRecord/jenkins");
        }
        if (type.contains("email")) {
            redisTemplate.delete("MessageRecord/email");
        }
        if (type.contains("question")) {
            redisTemplate.delete("MessageRecord/question");
        }
        redisTemplate.delete(key);

        return mapper.insertSelective(msg);
    }

    @Override
    public List<MessageCenter> FindMessageRecordByType(String type) {

        RedisSerializer redisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(redisSerializer);
        String key = "MessageRecord/" + type;
        List<MessageCenter> List = (List<MessageCenter>) redisTemplate.opsForValue().get(key);
        if (List == null) {
            List = mapper.selectByType("%" + type + "%");
            redisTemplate.opsForValue().set(key, List, 120, TimeUnit.SECONDS);
            //  System.out.println(key+" 缓存中没找到，访问数据库！");
        }
        //  else System.out.println(key+" 缓存中找到了，无需访问数据库！");
        return List;
    }
}
